﻿class WasGrid extends BaseGrid {
    NrOfCells: number;
    Width: number;
    Height: number;
    cells: Array<ICell>;
    drawGrid: IDrawGrid;
    getCells: IGetCell;
    NrOfIteration: number;

    constructor(createGrid: ICreateGrid, initGrid: IInitGrid, drawGrid: IDrawGrid, getCells: IGetCell) {
        super();

        this.cells = createGrid.Execute();
        this.Width = createGrid.Width;
        this.Height = createGrid.Height;
        initGrid.Execute(this);
        this.drawGrid = drawGrid;
        this.drawGrid.Init(this);
        this.getCells = getCells;
        this.getCells.Init(this);
        this.NrOfIteration = 0;
    }

    GetCellRaw(x: number, y: number) {
        return this.cells[x + y * this.Width];
    }

    GetCell(x: number, y: number) {
        return this.getCells.Execute(x, y);
    }

    GenerateContent(drawArea: IDrawArea) {
        this.RandomIteration(50);

        this.drawGrid.Draw(drawArea);
    }

    RandomIteration(percentage: number) {
        var numberOfCellsToRecalculate = this.Width * this.Height * percentage / 100;

        for (var i = 0; i < numberOfCellsToRecalculate; i++) {
            var x = Math.floor(Math.random() * this.Width);
            var y = Math.floor(Math.random() * this.Height);

            this.GetCellRaw(x, y).CalculateNextState();
            this.GetCellRaw(x, y).TakeOverNextState();
        }
    }




} 